package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.dao.OpenCityDao;
import com.qc.entity.OpenCityEntity;

@Repository("openCityDao")
public class OpenCityDaoImpl implements OpenCityDao{

	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public int saveOpenCity(OpenCityEntity openCityEntity) {
		
		String sql = "insert into t_open_city(province,city,region)values(?,?,?)";
		
		return jdbcTemplate.update(sql,new Object[]{openCityEntity.getProvince(),openCityEntity.getCity(),openCityEntity.getRegion()});
	}

	@Override
	public int deleteOpenCityId(long id) {
		String sql = "delete from t_open_city where b_id =" + id;
		
		return jdbcTemplate.update(sql);
	}

	@Override
	public int getListOpenCityCount() {
		
		String sql = "select count(*) from t_open_city where 1 = 1 and if_del = 0 ";
		
		return jdbcTemplate.queryForInt(sql);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<OpenCityEntity> getAllOpenCity(int pageNo, int pageSize) {
		
		int beginRow = (pageNo - 1)* pageSize;
        
		String sql = "select * from t_open_city where 1 = 1 and if_del = '0'  ";
        
        sql += " order by created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(OpenCityEntity.class));
	}

}
